from django.db.models import Q
from rest_framework import viewsets
from ERP_system.utils.multiple_destory import MultipleDestoryMixin,MultipleOpenMixin
from ERP_system.utils.pagination import GlobalPagination
from goods_info.models import *
from goods_info.serializer.category_serializer import CategorySerializer


class CategoryView(viewsets.ModelViewSet,MultipleDestoryMixin,MultipleOpenMixin):

    queryset = GoodsCategoryModel.objects.all()

    serializer_class = CategorySerializer


   # pagination_class = GlobalPagination


#过滤查询,根据传参pid 判断将要查询的内容，如果没有传参或者传参为0 那么将查询所有的父菜单，如果传参根据传参内容查询相应的子菜单
    def get_queryset(self):

        if self.action=='list':
            #获取pid
            pid=self.request.query_params.get('pid',None)
            #根据pid内容进行过滤判断
            if pid :
                if pid!='0':
                    pid = int(pid)
                    return GoodsCategoryModel.objects.filter(parent__id=pid).all()

                return GoodsCategoryModel.objects.filter(parent__isnull=True).all()


            else:

                return GoodsCategoryModel.objects.all()
        else:

           return  GoodsCategoryModel.objects.all()
